﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SignalRvideo.Models;
using OpenCvSharp;
using System.IO;

namespace SignalRvideo.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly WebSocketHandler _wsHandler;
        public HomeController(WebSocketHandler wsHandler,ILogger<HomeController> logger)
        {
            _wsHandler = wsHandler;
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        [DisableRequestSizeLimit]
        [HttpPost("/Home/Upts")]
        public async Task Upts()
        {
            int tsLen = 4096;
            byte[] tsData = new byte[tsLen];
            while(await Request.Body.ReadAsync(tsData, 0, tsLen)!=-1)
            { 
                await _wsHandler.SendDataToAllAsync(tsData);
            }
            return;
        }
    }
}
